package com.xb.cloud.order.service.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xb.cloud.common.model.ExceptionLoggerDetail;

@Aspect
@Component("baseOrderServiceAdvice")
public class BaseServiceAdvice {

	private final Logger LOGGER = LoggerFactory.getLogger(BaseServiceAdvice.class);

	@Pointcut("execution (* com.xb.cloud.order.service.*.*(..))")
	private void pointcut() {
	};

	@Around(value = "pointcut()")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		LOGGER.info("around");
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		try {
			return joinPoint.proceed();
		} catch (Throwable e) {
			ExceptionLoggerDetail loggerDetail = new ExceptionLoggerDetail(e, className, methodName, args);
			LOGGER.error(JSON.toJSONString(loggerDetail));
			// TODO:根据不同的exception类返回不同的错误码
			return null;
		}
	}
}
